<?php
set_time_limit(0);
$root=preg_replace('/cron$/', '', dirname(__FILE__));

require_once($root."inc.config.php");

require_once($root."initialize.php");

if(($db_msg=cron_initialize()) !== true){

	print_r("Connection error");

}else{ 

	$db=$vars["db"];

	
		$t = @mysql_num_rows($r=mysql_query("SELECT * FROM gg_users WHERE status='normal' and is_casino=0"));
		
		if($t<=0)
		{
			print_r("no record");
			exit;
		}
		
		$uid=0;
		$bal=0;
		while($user = mysql_fetch_assoc($r))
		{
			$url=parse_url('http://www.paradise-vegas.com/user/create');
			$host_port = $url['host'].(isset($url['port'])?":".$url['port']:"");
			$host = $url['host'];
			$port = isset($url['port'])?$url['port']:"80";
			$script = $url['path'];
										
			if($user['country']=='CN') $country=76;
			elseif($user['country']=='MY') $country=65;
			elseif($user['country']=='IND') $country=94;
			elseif($user['country']=='SG') $country=26;
			elseif($user['country']=='TH') $country=79;
			else $country=30;
			
			$datetime = date('Y-m-d H:i:s');
			$key = md5($user['username'].$datetime.'268194cab5022125a897d29d74ef574d');
			
			$header = "POST $script HTTP/1.1\r\n";
			$header .= "Host: $host_port\r\n";
			$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
			$request = "";
			$request .= "user[user_name]=".$user['username'];
			$request .= "&user[currency_id]=5";
			$request .= "&user[city]=".$user['city'];
			$request .= "&user[address1]=".$user['address'];
			$request .= "&user[address2]=".$user['address2'];
			$request .= "&user[first_name]=";
			$request .= "&user[last_name]=".$user['name'];
			$request .= "&user[password]=".$user['password'];
			$request .= "&user[password_confirmation]=".$user['password'];
			$request .= "&user[state]=".$user['state'];
			$request .= "&user[user_country_id]=".$country;
			$request .= "&user[cellphone]=".str_replace('-','',$user['mobileno']);
			$request .= "&user[zip]=".$user['zip'];
			$request .= "&user[api_time_stamp]=".$datetime;
			$request .= "&user[api_key]=".$key;
			$header .= "Content-length: ".strlen($request)."\r\n";
			$header .= "Connection: close\r\n\r\n";
			
			
			$ch = curl_init();
			//curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
			curl_setopt($ch, CURLOPT_URL,"http://www.paradise-vegas.com/user/create");
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $request);			
			ob_start();      // prevent any output
			curl_exec ($ch); // execute the curl command
			ob_end_clean();  // stop preventing output			
			curl_close ($ch);
			unset($ch);			
			//$ch = curl_init();
			//curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
			//curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");
			//curl_setopt($ch, CURLOPT_URL,"http://www.myterminal.com/list.asp");			
			//$buf2 = curl_exec ($ch);			
			//curl_close ($ch);			
			//echo "<PRE>".htmlentities($buf2);	
			
			
			/*
			//Now we open up the connection
			$socket = @fsockopen($host, $port, $errno, $errstr);
			if ($socket){
				fputs($socket, $header.$request); // send the details over
				while(!feof($socket)){
					$output[] = fgets($socket); //get the results
					echo fgets($socket); echo "<br>";
				}
				fclose($socket);
			}else{
				$err_msg = "unable to send out sms to provider";
				echo $err_msg;
			}
			/*
			$returned = split("&",$output[8]);
			foreach ($returned as $return_rst){
				$rst_split = split("=",$return_rst);
				if($rst_split[0] == "status"){
					$status = $rst_split[1];								
				}
				if($rst_split[0] == "msgid"){
					$msgid = $rst_split[1];
				}
				if($rst_split[0] == "err_msg"){
					$error = $rst_split[1];
				}
			}
			*/
			
			$sql = "UPDATE gg_users SET is_casino=1 WHERE id=".$user['id'];
			mysql_query($sql);
			exit;
		}

	print_r("finish");
}
?>